package com.wms.wms.controller.product;

import com.github.pagehelper.PageInfo;
import com.wms.wms.entity.Product;
import com.wms.wms.entity.Result;
import com.wms.wms.entity.Type;
import com.wms.wms.exception.*;
import com.wms.wms.service.prouct.ProductService;
import com.wms.wms.vo.request.ProductRequestVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;


/**
 * @author Chl
 * @version 1.0
 * @date 2019/12/18 12:31
 */
@RestController
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private ProductService productService;

    @GetMapping("/searchTypeById/{id}")
    public Result searchTypeById(@PathVariable String id) throws NotFoundException {
        Type type = productService.selectTypeById(id);
        return new Result(true,"查询成功",type);
    }

    @GetMapping("/searchTypeByName/{name}")
    public Result searchTypeByName(@PathVariable String name,Integer start,Integer size) throws NotFoundException {
        PageInfo<Type> types = productService.selectTypeByName(name, start, size);
        return new Result(true,"查询成功",types);
    }

    @GetMapping("/selectAll")
    public Result selectAll(Integer start,Integer size) throws NotFoundException {
        PageInfo<Type> typeAll = productService.selectTypeAll(start, size);
        return new Result(true,"查询成功",typeAll);
    }

    @DeleteMapping("/deleteType/{id}")
    public Result deleteType(@PathVariable String id) throws SystemException, CannotDeleteException {
        productService.deleteTypeById(id);
        return new Result(true,"删除成功");
    }

    @PutMapping("/updateType")
    public Result updateType(@RequestBody Type type) throws SystemException {
        productService.updateType(type);
        return new Result(true,"修改成功");
    }

    @PostMapping("/addType")
    public Result addType(@RequestBody Type type) throws AlreadyExistsException, SystemException {
        productService.addType(type);
        return new Result(true,"添加成功");
    }

    @GetMapping("/searchProductById/{id}")
    public Result searchProductById(@PathVariable String id) throws NotFoundException {
        Product product = productService.selectProductById(id);
        return new Result(true,"查询成功",product);
    }

    @GetMapping("/searchProductByName/{name}")
    public Result searchProductByName(@PathVariable String name,Integer start,Integer size) throws NotFoundException {
        PageInfo<Product> products = productService.selectProductByName(name, start, size);
        return new Result(true,"查询成功",products);
    }

    @PutMapping("/updateProduct")
    public Result updateProduct(@RequestBody @Validated ProductRequestVO vo) throws ErrorTypeIdException, SystemException {
        productService.updateProduct(vo);
        return new Result(true,"修改成功");
    }

    @PostMapping("/addProduct")
    public Result addProduct(@RequestBody @Valid ProductRequestVO vo) throws ErrorTypeIdException, SystemException, AlreadyExistsException {
        productService.addProduct(vo);
        return new Result(true,"添加成功");
    }

    @DeleteMapping("/deleteProduct/{id}")
    public Result deleteProduct(@PathVariable String id) throws SystemException, CannotDeleteException {
        productService.deleteProductById(id);
        return new Result(true,"删除成功");
    }

}
